package com.mac.intellect_flow.admin.controller.system;

import com.mac.intellect_flow.framework.dto.SysDictDataDto;
import com.mac.intellect_flow.reply.PageParam;
import com.mac.intellect_flow.reply.PageResult;
import com.mac.intellect_flow.reply.R;
import com.mac.intellect_flow.sys.entity.SysDictData;
import com.mac.intellect_flow.sys.service.SysDictDataService;
import com.mac.intellect_flow.sys.service.SysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 数据字典信息
 * 
 * @author mac
 */
@Tag(name = "字典数据管理")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {
    @Autowired
    private SysDictDataService dictDataService;

    @Autowired
    private SysDictTypeService dictTypeService;

    @Operation(summary = "字典数据分頁列表")
    @PreAuthorize("@ps.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public R<PageResult<SysDictData>> list(SysDictData dictData, PageParam pageParam) {
        return R.success(dictDataService.queryPage(dictData, pageParam));
    }

/*    @PreAuthorize("@ps.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }*/

    /**
     * 查询字典数据详细
     */
    @Operation(summary = "查询字典数据详细")
    @PreAuthorize("@ps.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public R getInfo(@PathVariable Long dictCode) {
        return R.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Operation(summary = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public R<List<SysDictDataDto>> dictType(@PathVariable String dictType) {
        List<SysDictDataDto> data = dictTypeService.selectDictDataByType(dictType);
        if (Objects.isNull(data)) {
            data = new ArrayList<SysDictDataDto>();
        }
        return R.success(data);
    }

    /**
     * 新增字典类型
     */
    @Operation(summary = "新增字典类型")
    @PreAuthorize("@ps.hasPermi('system:dict:add')")
    @PostMapping
    public R add(@Validated @RequestBody SysDictData dict) {
        return R.success(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @Operation(summary = "修改保存字典类型")
    @PreAuthorize("@ps.hasPermi('system:dict:edit')")
    @PutMapping
    public R edit(@Validated @RequestBody SysDictData dict) {
        return R.success(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @Operation(summary = "删除字典类型")
    @PreAuthorize("@ps.hasPermi('system:dict:remove')")
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.success();
    }
}
